﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace TestLambda
{
    class Program
    {
        static void Main(string[] args)
        {
            //计算(Price-5)*Count*Rebate
            ParameterExpression paraPrice = Expression.Parameter(typeof(decimal), "price");
            ConstantExpression constant = Expression.Constant(5m, typeof(decimal));
            BinaryExpression result1 = Expression.Subtract(paraPrice, constant);

            ParameterExpression paraCount = Expression.Parameter(typeof(decimal), "count");
            ParameterExpression paraRebate = Expression.Parameter(typeof(decimal), "rebate");
            BinaryExpression result2 = Expression.Multiply(paraCount, paraRebate);

            BinaryExpression result3 = Expression.Multiply(result1, result2);
            Expression<Func<decimal, decimal, decimal, decimal>> totalPrice = Expression.Lambda<Func<decimal, decimal, decimal, decimal>>(result3, paraPrice, paraCount, paraRebate);
            Func<decimal, decimal, decimal, decimal> myFun = totalPrice.Compile();
            Console.WriteLine(myFun(125m, 10m, 0.5m));
            Console.ReadKey();

        }

    }
}
